home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ghostscript.idb / usr / freeware / lib / ghostscript / 3.33 / gslp.ps.z / gslp.ps
Encoding:
Text File  |  1998-05-21  |  14.1 KB  |  506 lines

  1. %    Copyright (C) 1991, 1995 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of GNU Ghostscript.
  3. % GNU Ghostscript is distributed in the hope that it will be useful, but
  4. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  5. % anyone for the consequences of using it or for whether it serves any
  6. % particular purpose or works at all, unless he says so in writing.  Refer
  7. % to the GNU Ghostscript General Public License for full details.
  8.  
  9. % gslp.ps - format and print text
  10.  
  11. % This utility provides functionality approximately equivalent to the Unix
  12. % `enscript' program.  It prints plain text files using a single font.
  13. % It currently handles tabs and formfeeds, but not backspaces.
  14. % It only works with fixed-pitch fonts.
  15. % Standard flags implemented:
  16. %    -12BclqRr -b -f -F -L -p
  17. % Sun flags implemented:
  18. %    -T<n>    set tab width
  19. % Flags ignored:
  20. %    -GghKkmow -# -C -d -J -n -P -S -s -t -v
  21. % Flags added:
  22. %    --detect
  23. %        treats the file as PostScript if it starts with %!
  24. %    --(heading|footing)-(left|center|right) <string>
  25. %        sets the heading/footing fields; use -B first to clear
  26. %    --first-page <n>
  27. %        sets the first page to print
  28. %    --last-page <n>
  29. %        sets the last page to print
  30. %    --spacing <n>
  31. %        for double (n=2), triple (n=3), etc. spacing
  32. % Also, the string %# in a heading or footing is replaced with the page #.
  33. /PageNumberString (%#) def
  34.  
  35. /lpdict 150 dict def
  36. lpdict begin
  37.  
  38. % build iso-latin-1 version of a font
  39. /font-to-iso-latin-1 {    % <font> font-to-iso-latin-1 <font>
  40.     %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  41.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  42.     /Encoding ISOLatin1Encoding def currentdict end
  43.     dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn 
  44.     exch definefont
  45. } def
  46.  
  47. /find-latin-font {    % <name> find-latin-font <font>
  48.   findfont font-to-iso-latin-1
  49. } def
  50.  
  51. % Define the initial values of the printing parameters.
  52.  
  53. /BodyFont null def        % use default
  54.   /defaultBodyFont
  55.     { /Courier find-latin-font Landscape { 7 } { 10 } ifelse scalefont } def
  56. /Columns 1 def
  57. /DetectFileType false def
  58. /Footers false def
  59. /FootingLeft () def
  60. /FootingCenter () def
  61. /FootingRight () def
  62. /Headers true def
  63. /HeadingLeft () def
  64. /HeadingCenter () def
  65. /HeadingRight (page ) PageNumberString concatstrings def
  66. /HeadingFont null def        % use default
  67.   /defaultHeadingFont
  68.     { /Courier-Bold find-latin-font 10 scalefont } def
  69. /Landscape false def
  70. /MarginBottom 36 def        % 1/2"
  71. /MarginLeft 36 def        % 1/2"
  72. /MarginRight 36 def        % 1/2"
  73. /MarginTop 36 def        % 1/2"
  74. /MaxLines 9999 def        % max lines per page
  75. /Noisy true def            % i.e., not quiet
  76. /OutFile null def        % null = write directly to device
  77. /PageFirst 1 def
  78. /PageLast 99999 def
  79. /Spacing 1 def
  80. /Tab 8 def
  81. /Truncate false def        % wrap long lines, don't truncate
  82.  
  83. % When writing to a file, we want to write out PostScript;
  84. % when writing to the printer, we want to execute it;
  85. % some commands should be executed regardless.
  86. % lpexec provides for all this.
  87.  
  88. /lpexec        % <arg1> ... <argn> </op> <n> <do_always> lpexec -
  89.  { OutFile null eq
  90.     { pop 1 add true
  91.     }
  92.     { /t exch def 1 add /n exch def cvx
  93.       n { n -1 roll dup wosp } repeat
  94.       OutFile (\n) writestring
  95.       n t
  96.     }
  97.    ifelse
  98.     { pop load exec }
  99.     { { pop } repeat }
  100.    ifelse
  101.  } def
  102.  
  103. /lpmoveto
  104.  { /moveto 2 true lpexec
  105.  } def
  106. /lpshow
  107.  { dup length 0 ne { /show 1 false lpexec } { pop } ifelse
  108.  } def
  109. /lpsetmyfont
  110.  { dup load setfont
  111.    OutFile null ne { cvx /setfont 1 false lpexec } { pop } ifelse
  112.  } def
  113.  
  114. % Define some utility procedures.
  115.  
  116. /banner        % ypos left center right
  117.  { /HFont lpsetmyfont
  118.    3 -1 roll bannerstring pop 0 4 index pwidth showline2 pop
  119.    exch bannerstring pwidth exch sub 2 div 3 index pwidth showline2 pop
  120.    bannerstring pwidth exch sub
  121.    3 -1 roll pwidth showline2 pop
  122.  } def
  123.  
  124. /bannerstring    % string -> string width
  125.   { PageNumberString search
  126.      { exch pop pindex 4 string cvs concatstrings exch concatstrings
  127.      }
  128.     if dup stringwidth pop
  129.   } def
  130.  
  131. /beginpage
  132.  { /lindex 0 def
  133.    /skipping pindex PageFirst ge pindex PageLast le and not def
  134.    /save 0 true lpexec /pagesave exch def
  135.    skipping { nulldevice   /OutFile null def } if
  136.    Headers
  137.     { lheight hdescent add
  138.       HeadingLeft HeadingCenter HeadingRight banner
  139.     } if
  140.    /BFont lpsetmyfont
  141.  } def
  142.  
  143. /endpage
  144.  { lindex 0 ne
  145.     { Footers
  146.        { topskip plength sub hdescent add
  147.          FootingLeft FootingCenter FootingRight banner
  148.        } if
  149.       /showpage 0 false lpexec
  150.     } if
  151.    pagesave /restore 0 true lpexec
  152.    /pindex pindex 1 add def
  153.  } def
  154.  
  155. /fontheight    % <font> fontheight <ascent> <height>
  156.  { gsave setfont
  157.    newpath 0 0 moveto
  158.    (|^_j) false charpath
  159.    pathbbox exch pop dup 2 index sub 4 -2 roll pop pop
  160.    grestore exch 1.25 mul exch 1.25 mul
  161.  } def
  162.  
  163. /wosp
  164.  { OutFile ( ) writestring OutFile exch write==only
  165.  } def
  166.  
  167. /outfont        % name font ->
  168.  { OutFile null ne
  169.     { exch wosp
  170.       dup /FontName get 
  171.       dup wosp OutFile ( findfont) writestring
  172.       %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  173.       OutFile ( 
  174.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  175.     /Encoding ISOLatin1Encoding def currentdict end
  176.       ) writestring
  177.       wosp OutFile (-ISOLatin1 exch definefont) writestring
  178.       /FontMatrix get 0 get 1000 mul round cvi wosp
  179.       OutFile ( scalefont def\n) writestring
  180.     }
  181.     { pop pop
  182.     }
  183.    ifelse
  184.  } def
  185.  
  186. /StringFF (\f) def
  187. /CharFF StringFF 0 get def
  188. /StringTAB (\t) def
  189. /CharTAB StringTAB 0 get def
  190.  
  191. /showline        % line -> leftover_line (handles \f)
  192.  {  { showline1 dup length 0 eq { exit } if
  193.       dup 0 get CharFF ne { exit } if
  194.       endpage beginpage
  195.       skip1
  196.     }
  197.    loop
  198.  } def
  199.  
  200. /showline1        % line -> leftover_line (handles page break)
  201.  { lindex llength eq { endpage beginpage } if
  202.    lindex colines idiv cowidth mul        % x
  203.    lindex colines mod 1 add lheight mul neg fascent sub    % y
  204.    1 index cowidth add
  205.    showline2
  206.    /lindex lindex 1 add def
  207.  } def
  208.  
  209. /showline2    % string x y xlimit -> leftover_string (handles tabs)
  210.  { 2 index exch 5 2 roll lpmoveto    % xinit xlimit string
  211.     { showline3 dup length 0 eq { exit } if
  212.       dup 0 get CharTAB ne { exit } if
  213.       currentpoint exch 4 index sub tabwx div
  214.     0.05 add ceiling tabwx mul 4 index add exch lpmoveto
  215.       skip1
  216.       currentpoint pop 2 index ge { exit } if
  217.     }
  218.    loop exch pop exch pop
  219.  } def
  220.  
  221. /showline3    % xlimit string -> xlimit leftover_string
  222.         % (finds line break / tab / formfeed)
  223.  { currentpoint pop 2 index exch sub
  224.      cwx div 0.1 add cvi 0 max 1 index length min
  225.    1 index 0 3 -1 roll getinterval
  226.     % look for \f or \t
  227.    StringFF search { exch pop exch pop } if
  228.    StringTAB search { exch pop exch pop } if
  229.    dup lpshow
  230.    length dup 2 index length exch sub getinterval
  231.  } def
  232.  
  233. /skip1
  234.  { dup length 1 sub 1 exch getinterval
  235.  } def
  236.  
  237. /e= {        % <object> e= - -- print an object to stderr
  238.   (%stderr) (w) file dup 3 -1 roll write=only flushfile
  239. } def
  240.  
  241. /eprint {    % <string> eprint - -- print a string to stderr
  242.   (%stderr) (w) file dup 3 -1 roll writestring flushfile
  243. } def
  244.  
  245. % The main printing procedure
  246.  
  247. /lp        % file initial_chars ->
  248.  { /lpline exch def
  249.    /lpfile exch def
  250.    /save 0 true lpexec
  251.  
  252. % Initialize the device and fonts.
  253.    /BFont
  254.      BodyFont null eq { defaultBodyFont } { BodyFont } ifelse def
  255.    /BFont BFont outfont
  256.    Headers Footers or
  257.     { /HFont
  258.     HeadingFont null eq { defaultHeadingFont } { HeadingFont } ifelse def
  259.       /HFont HFont outfont
  260.     }
  261.    if
  262.  
  263. % Get the layout parameters.
  264.    clippath
  265.    Landscape { -90 /rotate 1 true lpexec } if
  266.    BFont setfont ( ) stringwidth pop /cwx exch def
  267.      cwx Tab mul /tabwx exch def
  268.    BFont fontheight /fheight exch def /fascent exch def
  269.    Headers Footers or { HFont fontheight } { 0 0 } ifelse
  270.      /hheight exch def /hascent exch def
  271.      /hdescent hheight hascent sub def
  272.    fheight Spacing mul /lheight exch def
  273.    Headers { hheight lheight add } { 0 } ifelse
  274.      /topskip exch def
  275.    Footers { hheight lheight add } { 0 } ifelse
  276.      /botskip exch def
  277.    /pskip topskip botskip add def
  278.     % Translate the page so that (0,0) corresponds to
  279.     % the top of the topmost body line.
  280.    pathbbox
  281.      2 index sub MarginBottom MarginTop add sub /plength exch def
  282.      2 index sub MarginLeft MarginRight add sub /pwidth exch def
  283.      pwidth Columns div /cowidth exch def
  284.      exch MarginLeft add
  285.      exch MarginBottom add plength add topskip sub
  286.      /translate 2 true lpexec
  287.    plength pskip sub lheight div cvi MaxLines min
  288.      dup /colines exch def
  289.      Columns mul /llength exch def
  290.    OutFile null ne { nulldevice } if
  291.  
  292. % Print layout
  293.    Noisy
  294.     { (Page height = ) eprint llength e=
  295.       (.\n) eprint flush
  296.     } if
  297.  
  298. % Disable stack recording so we can use stopped with readline.
  299.    $error /recordstacks false put
  300.  
  301. % Initialize for the first page.
  302.    /lbuf 1000 string def
  303.    /pindex 1 def
  304.    beginpage
  305.  
  306. % Iterate through the file.
  307.    lpline
  308.     { dup length /pos exch def
  309.       lbuf exch 0 exch putinterval
  310.        { lpfile lbuf pos lbuf length pos sub getinterval readline } stopped
  311.        {    % Filled the line before a CR or EOF.
  312.          exch pop showline
  313.        }
  314.        {    % Reached CR and/or EOF first.
  315.          exch length pos add lbuf exch 0 exch getinterval
  316.      1 index { showline } if        % omit final empty line
  317.       { dup length 0 eq Truncate or { pop () exit } if
  318.         showline
  319.       }
  320.      loop
  321.      exch not { exit } if
  322.        }
  323.       ifelse
  324.     } loop
  325.    pop
  326.  
  327. % Wrap up.
  328.    endpage
  329.    /restore 0 true lpexec
  330.  
  331.  } def
  332.  
  333. end
  334.  
  335. % Usage: <file> lp
  336. %   prints <file> using the current parameter settings.
  337. % Usage: [ <arg1> ... <argn> ] lpcommand
  338. %   interprets args like a command line.
  339.  
  340. /lp { save   lpdict begin () lp end   restore } def
  341.  
  342. lpdict begin
  343.  
  344. /splitfn        % (FontNNN) -> <font>
  345.  { dup /arg exch def length
  346.     { dup 0 le { exit } if
  347.       dup 1 sub arg exch get dup 48 ge exch 59 le and not { exit } if
  348.       1 sub
  349.     } loop
  350.    arg exch 0 exch getinterval dup cvn find-latin-font
  351.    exch arg exch anchorsearch pop pop cvr scalefont
  352.  } def
  353.  
  354. % Parse the command line switches.
  355.  
  356. /doswitch    % argn ... arg1 (-?) restofswitch ->
  357.  { exch dup cvn lpdict exch known
  358.     { cvn load exec }
  359.     { exch pop (Unknown switch: ) eprint eprint (\n) eprint }
  360.    ifelse
  361.  } def
  362.  
  363. /more        % argn ... arg1 restofswitch ->
  364.  { dup length 0 ne
  365.     { (- ) dup 1 3 index 0 get put
  366.       exch dup length 1 sub 1 exch getinterval
  367.       doswitch
  368.     }
  369.     { pop
  370.     }
  371.    ifelse
  372.  } def
  373.  
  374. /-- { (--) exch concatstrings
  375.       dup cvn lpdict exch known
  376.        { cvn load exec }
  377.        { (Unknown switch: ) eprint eprint (\n) eprint }
  378.       ifelse
  379.     } def
  380. /--columns { cvi 1 max /Columns exch def } def
  381. /--detect { /DetectFileType true def } def
  382. /--first-page { cvi /PageFirst exch def } def
  383. /--footing-center { /FootingCenter exch def   /Footers true def } def
  384. /--footing-left { /FootingLeft exch def   /Footers true def } def
  385. /--footing-right { /FootingRight exch def   /Footers true def} def
  386. /--heading-center { /HeadingCenter exch def   /Headers true def } def
  387. /--heading-left { /HeadingLeft exch def   /Headers true def } def
  388. /--heading-right { /HeadingRight exch def   /Headers true def } def
  389. /--margin-bottom { cvr 72.0 mul /MarginBottom exch def } def
  390. /--margin-left { cvr 72.0 mul /MarginLeft exch def } def
  391. /--margin-right { cvr 72.0 mul /MarginRight exch def } def
  392. /--margin-top { cvr 72.0 mul /MarginTop exch def } def
  393. /--last-page { cvi /PageLast exch def } def
  394. /--spacing { cvr /Spacing exch def } def
  395.  
  396. /-# { pop } def        % ignore
  397. /-+ { -- } def
  398. (-1)cvn { /Columns 1 def   more } def
  399. (-2)cvn { /Columns 2 def   more } def
  400. /-b { /HeadingLeft exch def   /HeadingCenter () def   /HeadingRight PageNumberString def
  401.       /Headers true def
  402.       /break true def
  403.     } def
  404. /-B { /HeadingLeft () def   /HeadingCenter () def   /HeadingRight () def
  405.       /Headers false def
  406.       /FootingLeft () def   /FootingCenter () def   /FootingRight () def
  407.       /Footers false def
  408.       /break true def
  409.       more
  410.     } def
  411. /-C { pop } def        % ignore
  412. /-c { /Truncate true def   more } def
  413. /-d { pop } def        % ignore
  414. /-f { splitfn /BodyFont exch def } def
  415. /-F { splitfn /HeadingFont exch def } def
  416. /-G { more } def    % ignore
  417. /-g { more } def    % ignore
  418. /-h { more } def    % ignore
  419. /-J { pop } def        % ignore
  420. /-K { more } def    % ignore
  421. /-k { more } def    % ignore
  422. /-l { 66 -L -B } def
  423. /-L { cvi /MaxLines exch def } def
  424. /-m { more } def    % ignore
  425. /-n { pop } def        % ignore
  426. /-o { more } def    % ignore
  427. /-p { (w) file /OutFile exch def   OutFile (%!\n) writestring } def
  428. /-P { pop } def        % ignore
  429. /-q { /Noisy false def   more } def
  430. /-r { /Landscape true def   more } def
  431. /-R { /Landscape false def   more } def
  432. /-S { pop } def        % ignore
  433. /-s { pop } def        % ignore
  434. /-T { cvi /Tab exch def } def
  435. /-v { pop } def        % ignore
  436. /-w { more } def    % ignore
  437.  
  438. /lp1        % filename ->
  439.  { break not { dup /HeadingLeft exch def } if
  440.    Noisy
  441.     { (Printing ) eprint dup eprint (\n) eprint 
  442.     } if
  443.    (r) file
  444.         % If requested, check for a PostScript file.
  445.    DetectFileType
  446.     { dup 2 string readstring pop dup (%!) eq
  447.        {    % Yes, it's a PostScript file.
  448.          pop dup 80 string readline pop pop cvx exec
  449.        }
  450.        { lp
  451.        }
  452.       ifelse
  453.     }
  454.     { () lp
  455.     }
  456.    ifelse
  457.  } bind def
  458.  
  459. /lpcstring 100 string def
  460.  
  461. end
  462.  
  463. /lpcommand        % [arg1 ... argn] -> -
  464.  {    % Push the commands on the stack in reverse order
  465.    mark exch
  466.    dup length 1 sub -1 0 { 1 index exch get exch } for pop
  467.    lpdict begin
  468.    /break false def
  469.     { dup mark eq { pop exit } if
  470.       dup length 2 ge { dup 0 get (-) 0 get eq } { false } ifelse
  471.        { dup 0 2 getinterval
  472.          exch dup length 2 sub 2 exch getinterval
  473.      doswitch
  474.        }
  475.        { dup  /matched false def
  476.           { /matched true def    lp1 } lpcstring filenameforall
  477.      matched { pop } { lp1 } ifelse        % let the error happen
  478.        }
  479.       ifelse
  480.     } loop
  481.    OutFile null ne
  482.     { OutFile (%stdout) (w) file ne { OutFile closefile } if
  483.       /OutFile null def
  484.     } if
  485.    end
  486.  } def
  487.  
  488. [ shellarguments
  489.  { ] dup length 0 ne
  490.     { lpcommand
  491.     }
  492.     { (Usage: gslp [-12BclqRr] [-b<header] [-f<font>] [-F<hfont>]\n) eprint
  493.       (        [-L<lines>] [-p<outfile>] [-T<tabwidth>] [--columns <n>]\n) eprint
  494.       (        [--detect] [--first-page <page#>] [--last-page <page#>]\n) eprint
  495.       (        [--(heading|footing)-(left|right|center) <string>]\n) eprint
  496.       (        [--margin-(top|bottom|left|right) <inches>]\n) eprint
  497.       (        [--spacing <n>] file1 ... filen\n) eprint
  498.     }
  499.    ifelse
  500.  }
  501.  { pop }
  502. ifelse
  503.